﻿package mortal.game.view.guildNew.other
{
    import Message.DB.Tables.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.guildCopy.*;
    import mortal.game.view.copy.guildCopy.renderer.*;
    import mortal.game.view.guildNew.*;
    import mortal.game.view.guildNew.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildNewCopyPanel extends BaseWindow
    {
        private var _LinePointArr:Array;
        private var _panelBg:GBitmap;
        private var _leftTopContianer:GSprite;
        private var _line:GBitmap;
        private var _lineBitmapData:BitmapData;
        private var _btnOpenCopy:GLoadingButton;
        private var _btnEnterCopy:GLoadingButton;
        private var _ballLv:GBitmap;
        private var _ballLan:GBitmap;
        private var _ballZi:GBitmap;
        private var _ballChen:GBitmap;
        private var _ballAnjin:GBitmap;
        private var _ballArr:Array;
        private var _textArr:Array;
        private var _curActiveContianer:GSprite;
        private var _curActiveDescBg:GBitmap;
        private var _curActiveDesc:GTextFiled;
        private var _nextActiveDesc:GTextFiled;
        private var _actionDesc:GTextFiled;
        private var _itemList:GTileList;
        private var uiInitOver:Boolean;
        private var _curCopyConfig:TGuildBossCopy;
        private var _img2d:Img2D;
        private var _rect3d:Rect3DObject;
        protected var _bodyPlayer:ActionPlayer;
        private static var _instance:GuildNewCopyPanel;

        public function GuildNewCopyPanel(param1:ILayer = null)
        {
            this._LinePointArr = [[7, 3], [22, 71], [51, 139], [93, 204], [144, 257]];
            this.layer = LayerManager.windowLayer3D;
            if (_instance)
            {
                throw new Error("单例");
            }
            setSize(722, 526);
            title = "" + Language.getString(71555);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_activity_requst));
            LoaderHelp.addResCallBack(ResFileConst.guildCopyPanel, this.showSkin);
            return;
        }// end function

        private function showSkin() : void
        {
            var _loc_5:TGuildBossCopy = null;
            var _loc_6:GTextFiled = null;
            if (isDisposed)
            {
                return;
            }
            pushUIToDisposeVec(UIFactory.bg(10, 30, 702 + 10, 476 + 10, this));
            this._panelBg = UIFactory.bitmap("", 15, 35, this);
            LoaderManager.instance.load(ImagesConst.GuildCopyBg + ".swf", this.onBgLoadComplet);
            this._btnEnterCopy = UIFactory.gLoadingButton(ResFileConst.GuildCopyEnterBtn, 490, 392, 113, 37, contentTopOf3DSprite);
            this._btnOpenCopy = UIFactory.gLoadingButton(ResFileConst.GuildCopyOpenBtn, 490, 392, 113, 37);
            this._leftTopContianer = UIFactory.getUICompoment(GSprite, 52, 70, contentTopOf3DSprite);
            pushUIToDisposeVec(UIFactory.bitmap(ImagesConst.GuildCopyLineBg, 0, 0, this._leftTopContianer));
            this._line = UIFactory.bitmap("", 0, 0, this._leftTopContianer);
            var _loc_1:int = 21;
            this._ballLv = UIFactory.bitmap(ImagesConst.GuildCopyLvBall, this._LinePointArr[0][0] - _loc_1, this._LinePointArr[0][1] - _loc_1, this._leftTopContianer);
            this._ballLan = UIFactory.bitmap(ImagesConst.GuildCopyLanBall, this._LinePointArr[1][0] - _loc_1, this._LinePointArr[1][1] - _loc_1, this._leftTopContianer);
            this._ballZi = UIFactory.bitmap(ImagesConst.GuildCopyZiBall, this._LinePointArr[2][0] - _loc_1, this._LinePointArr[2][1] - _loc_1, this._leftTopContianer);
            this._ballChen = UIFactory.bitmap(ImagesConst.GuildCopyChenBall, this._LinePointArr[3][0] - _loc_1, this._LinePointArr[3][1] - _loc_1, this._leftTopContianer);
            this._ballAnjin = UIFactory.bitmap(ImagesConst.GuildCopyAnjinBall, this._LinePointArr[4][0] - _loc_1, this._LinePointArr[4][1] - _loc_1, this._leftTopContianer);
            this._ballArr = [];
            this._ballArr.push(this._ballLv);
            this._ballArr.push(this._ballLan);
            this._ballArr.push(this._ballZi);
            this._ballArr.push(this._ballChen);
            this._ballArr.push(this._ballAnjin);
            pushUIToDisposeVec(UIFactory.bitmap(ImagesConst.GuildCopyLvBg, this._ballLv.x + _loc_1 * 1, this._ballLv.y, this._leftTopContianer));
            pushUIToDisposeVec(UIFactory.bitmap(ImagesConst.GuildCopyLanBg, this._ballLan.x + _loc_1 * 1, this._ballLan.y, this._leftTopContianer));
            pushUIToDisposeVec(UIFactory.bitmap(ImagesConst.GuildCopyZiBg, this._ballZi.x + _loc_1 * 1, this._ballZi.y, this._leftTopContianer));
            pushUIToDisposeVec(UIFactory.bitmap(ImagesConst.GuildCopyChenBg, this._ballChen.x + _loc_1 * 1, this._ballChen.y, this._leftTopContianer));
            pushUIToDisposeVec(UIFactory.bitmap(ImagesConst.GuildCopyAnjinBg, this._ballAnjin.x + _loc_1 * 1, this._ballAnjin.y, this._leftTopContianer));
            this._textArr = [];
            this._textArr.push(UIFactory.textField("", this._ballLv.x + _loc_1 * 2 + 8, this._ballLv.y + 10, 140, 22, this._leftTopContianer));
            this._textArr.push(UIFactory.textField("", this._ballLan.x + _loc_1 * 2 + 8, this._ballLan.y + 10, 140, 22, this._leftTopContianer));
            this._textArr.push(UIFactory.textField("", this._ballZi.x + _loc_1 * 2 + 8, this._ballZi.y + 10, 140, 22, this._leftTopContianer));
            this._textArr.push(UIFactory.textField("", this._ballChen.x + _loc_1 * 2 + 8, this._ballChen.y + 10, 140, 22, this._leftTopContianer));
            this._textArr.push(UIFactory.textField("", this._ballAnjin.x + _loc_1 * 2 + 8, this._ballAnjin.y + 10, 140, 22, this._leftTopContianer));
            var _loc_2:* = GuildBossCopyConfig.instance.getConfigArr();
            var _loc_3:int = 0;
            while (_loc_3 < this._textArr.length)
            {
                
                _loc_5 = _loc_2[_loc_3] as TGuildBossCopy;
                _loc_6 = this._textArr[_loc_3] as GTextFiled;
                _loc_6.htmlText = HTMLUtil.addColor(_loc_5.active + Language.getString(71556), GlobalStyle.colorBai) + HTMLUtil.addColor(Language.getStringByParam(71557, _loc_5.time), GlobalStyle.colorLv);
                _loc_3++;
            }
            this._curActiveContianer = UIFactory.getUICompoment(GSprite, this._ballLan.x + 150, this._ballLan.y - 14, this._leftTopContianer);
            this._curActiveDescBg = UIFactory.bitmap(ImagesConst.GuildCopyTipBg, 0, 0, this._curActiveContianer);
            this._curActiveDesc = UIFactory.textField("", 16, 10, 190, 44, this._curActiveContianer);
            this._curActiveDesc.multiline = true;
            this._curActiveDesc.wordWrap = true;
            this._curActiveDesc.htmlText = HTMLUtil.addColor("" + Language.getString(71558), GlobalStyle.colorBai);
            this._nextActiveDesc = UIFactory.textField("", 16, 32, 190, 45, this._curActiveContianer);
            this._nextActiveDesc.multiline = true;
            this._nextActiveDesc.wordWrap = true;
            this._nextActiveDesc.htmlText = HTMLUtil.addColor("" + Language.getString(71559), GlobalStyle.colorBai);
            pushUIToDisposeVec(UIFactory.textField("" + Language.getString(71560), 30, 363, 100, 22, contentTopOf3DSprite, GlobalStyle.textFormatHuang));
            this._actionDesc = UIFactory.textField("" + Language.getString(71561), 30, 383, 410, 66, contentTopOf3DSprite);
            pushUIToDisposeVec(UIFactory.textField("" + Language.getString(71562), 30, 438, 100, 22, contentTopOf3DSprite, GlobalStyle.textFormatLv));
            this._itemList = UIFactory.tileList(30, 460, 300, 45, contentTopOf3DSprite);
            this._itemList.rowHeight = 45;
            this._itemList.columnWidth = 45;
            this._itemList.setStyle("cellRenderer", GuildCopyRewardItemListCellRenderer);
            var _loc_4:* = DailyRewardConfig.instance.getRewardByCode(104);
            this._itemList.dataProvider = new DataProvider(_loc_4);
            this._itemList.drawNow();
            this.uiInitOver = true;
            this.configEventListener(MouseEvent.CLICK, this.onMouseClickHandler);
            this.update();
            return;
        }// end function

        private function onMouseClickHandler(event:MouseEvent) : void
        {
            switch(event.target)
            {
                case this._btnOpenCopy:
                {
                    if (this.curCopyConfig == null)
                    {
                        MsgManager.showMouseMsg("" + Language.getString(71563), MsgRollTipsType.msgRollTips3);
                        return;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_copy_open, this.curCopyConfig.color));
                    break;
                }
                case this._btnEnterCopy:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_copy_enter));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function update() : void
        {
            var _loc_4:TGuildBossCopy = null;
            var _loc_5:GBitmap = null;
            var _loc_8:Array = null;
            var _loc_10:Number = NaN;
            if (!this.uiInitOver)
            {
                return;
            }
            this.updateCurCopyConfig();
            var _loc_1:* = Cache.instance.guild.selfGuildInfo;
            var _loc_2:* = Cache.instance.copy.guildCopy;
            this._btnEnterCopy.enabled = _loc_2.isOpen;
            this._btnEnterCopy.filterEnabled = _loc_2.isOpen;
            var _loc_3:* = GuildBossCopyConfig.instance.getConfigArr();
            var _loc_6:int = 0;
            var _loc_7:* = _loc_3.length - 1;
            _loc_8 = GuildNewUtil.getCurAltarCurLAndNeedL();
            var _loc_9:int = 0;
            while (_loc_9 < _loc_3.length)
            {
                
                _loc_4 = _loc_3[_loc_9] as TGuildBossCopy;
                _loc_5 = this._ballArr[_loc_9] as GBitmap;
                if (_loc_4.active > _loc_8[0])
                {
                    _loc_5.filters = [FilterConst.colorFilter2];
                    _loc_7 = Math.min(_loc_9, _loc_7);
                }
                else
                {
                    _loc_5.filters = [];
                    _loc_6 = Math.max(_loc_9, _loc_6);
                }
                _loc_9++;
            }
            if (_loc_3[_loc_7].active - _loc_3[_loc_6].active == 0)
            {
                _loc_10 = 1;
            }
            else
            {
                _loc_10 = (_loc_8[0] - _loc_3[_loc_6].active) / (_loc_3[_loc_7].active - _loc_3[_loc_6].active);
            }
            var _loc_11:* = UIFactory.bitmap(ImagesConst.GuildCopyLine);
            var _loc_12:* = this._LinePointArr[_loc_6][1] + (this._LinePointArr[_loc_7][1] - this._LinePointArr[_loc_6][1]) * _loc_10;
            if (_loc_12 <= 0)
            {
                _loc_12 = 1;
            }
            this._lineBitmapData = new BitmapData(_loc_11.width, _loc_12, true, 16777215);
            this._lineBitmapData.draw(_loc_11);
            this._line.bitmapData = this._lineBitmapData;
            if (this.curCopyConfig == null)
            {
                this._curActiveContianer.x = this._ballLv.x + 160;
                this._curActiveContianer.y = this._ballLv.y - 15;
                this._curActiveDesc.htmlText = HTMLUtil.addColor("" + Language.getString(71564), GlobalStyle.colorBai) + HTMLUtil.addColor("" + _loc_8[0] + Language.getString(71565), GlobalStyle.colorLv);
                this._nextActiveDesc.htmlText = Language.getString(71566) + HTMLUtil.addColor(Language.getStringByParam(71567, _loc_8[1]), GlobalStyle.colorHong);
            }
            else
            {
                this._curActiveContianer.x = this._ballArr[_loc_6].x + 160;
                this._curActiveContianer.y = this._ballArr[_loc_6].y - 15;
                this._curActiveDesc.htmlText = HTMLUtil.addColor(Language.getString(71568), GlobalStyle.colorBai) + HTMLUtil.addColor("" + _loc_8[0] + Language.getString(71565), GlobalStyle.colorLv);
                this._nextActiveDesc.htmlText = Language.getString(71566) + HTMLUtil.addColor(Language.getStringByParam(71567, _loc_8[1]), GlobalStyle.colorHong);
            }
            this._actionDesc.htmlText = Language.getStringByParam(71569, _loc_8[0], ActionTimeShowUtil.instance.GuildBossStr);
            this.update3DBoss();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            var _loc_3:GTextFiled = null;
            if (this.uiInitOver == false)
            {
                return;
            }
            this.uiInitOver = false;
            this._curCopyConfig = null;
            this._panelBg.dispose(param1);
            this._lineBitmapData.dispose();
            this._line.dispose(param1);
            this._btnOpenCopy.dispose(param1);
            this._btnEnterCopy.dispose(param1);
            this._ballLv.dispose(param1);
            this._ballLan.dispose(param1);
            this._ballZi.dispose(param1);
            this._ballChen.dispose(param1);
            this._ballAnjin.dispose(param1);
            var _loc_2:int = 0;
            while (_loc_2 < this._textArr.length)
            {
                
                _loc_3 = this._textArr[_loc_2] as GTextFiled;
                _loc_3.dispose(param1);
                _loc_2++;
            }
            this._textArr.splice(0);
            this._leftTopContianer.dispose(param1);
            this._curActiveDesc.dispose(param1);
            this._nextActiveDesc.dispose(param1);
            this._curActiveDescBg.dispose(param1);
            this._curActiveContianer.dispose(param1);
            this._actionDesc.dispose(param1);
            this._itemList.dispose(param1);
            if (this._rect3d)
            {
                if (this._bodyPlayer)
                {
                    this._rect3d.disposeObj3d(this._bodyPlayer);
                    this._bodyPlayer = null;
                }
                if (this._img2d)
                {
                    this._rect3d.removeImg(this._img2d);
                    this._img2d = null;
                }
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            this._panelBg = null;
            this._lineBitmapData = null;
            this._line = null;
            this._btnOpenCopy = null;
            this._btnEnterCopy = null;
            this._ballLv = null;
            this._ballLan = null;
            this._ballZi = null;
            this._ballChen = null;
            this._ballAnjin = null;
            this._ballArr.splice(0);
            this._leftTopContianer = null;
            this._curActiveDesc = null;
            this._nextActiveDesc = null;
            this._curActiveDescBg = null;
            this._curActiveContianer = null;
            this._actionDesc = null;
            this._itemList = null;
            super.disposeImpl(param1);
            return;
        }// end function

        protected function onBgLoadComplet(param1) : void
        {
            if (isDisposed)
            {
                return;
            }
            this._panelBg.bitmapData = param1.bitmapData as BitmapData;
            this.add3DBoss();
            return;
        }// end function

        protected function add3DBoss() : void
        {
            if (this._panelBg && this._rect3d == null)
            {
                this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(15, 35, 702, 476), this);
                Rect3DManager.instance.windowShowHander(null, this);
                if (this._panelBg.bitmapData)
                {
                    this._rect3d.removeImg(this._img2d);
                    this._img2d = new Img2D(null, this._panelBg.bitmapData, new Rectangle(0, 0, 702, 476), false);
                    this._rect3d.addImg(this._img2d);
                }
                this.update3DBoss();
            }
            return;
        }// end function

        public function update3DBoss() : void
        {
            if (this._rect3d == null || this._img2d == null)
            {
                return;
            }
            var _loc_1:* = this.curCopyConfig != null ? (this.curCopyConfig.ringBegin) : (GuildBossCopyConfig.instance.getConfigByColor(1).ringBegin);
            var _loc_2:* = int(CopyConfig.instance.getCopyInfoByCode(GuildConst.GuildCopyId).maps);
            var _loc_3:* = BossConfig.instance.getRefreshByMapIdAndRound(_loc_2, _loc_1);
            var _loc_4:* = BossConfig.instance.getInfoByCode(_loc_3.bossCode);
            if (_loc_4 == null)
            {
                return;
            }
            var _loc_5:* = _loc_4.mesh + ".md5mesh";
            var _loc_6:* = _loc_4.bone + ".skeleton";
            var _loc_7:* = _loc_4.texture;
            if (this._bodyPlayer && this._bodyPlayer.meshUrl == _loc_5 && this._bodyPlayer.animUrl == _loc_6)
            {
                return;
            }
            if (this._bodyPlayer)
            {
                this._rect3d.disposeObj3d(this._bodyPlayer);
            }
            this._bodyPlayer = FrObjectPool.getObject(ActionPlayer, this);
            this._bodyPlayer.changeAction(ActionName.Stand);
            this._bodyPlayer.selectEnabled = false;
            this._bodyPlayer.setRotation(0, 0, 0);
            this._bodyPlayer.load(_loc_5, _loc_6, _loc_7, this._rect3d.renderList);
            if (_loc_4.modelScale)
            {
                this._bodyPlayer.scaleValue = _loc_4.modelScale * 1 / 100;
            }
            this._bodyPlayer.play();
            this._rect3d.addObject3d(this._bodyPlayer, 520, 320);
            return;
        }// end function

        public function get curCopyConfig() : TGuildBossCopy
        {
            if (this._curCopyConfig == null)
            {
                this.updateCurCopyConfig();
            }
            return this._curCopyConfig;
        }// end function

        public function updateCurCopyConfig() : void
        {
            var _loc_2:TGuildBossCopy = null;
            var _loc_4:int = 0;
            var _loc_1:* = GuildBossCopyConfig.instance.getConfigArr();
            var _loc_3:int = 0;
            while (_loc_3 < _loc_1.length)
            {
                
                _loc_2 = _loc_1[_loc_3] as TGuildBossCopy;
                _loc_4 = GuildNewUtil.getCurAltarCurLAndNeedL()[0];
                if (_loc_2.active > _loc_4)
                {
                    this._curCopyConfig = GuildBossCopyConfig.instance.getConfigByColor((_loc_2.color - 1));
                    return;
                }
                _loc_3++;
            }
            this._curCopyConfig = _loc_1[(_loc_1.length - 1)];
            return;
        }// end function

        public static function get instance() : GuildNewCopyPanel
        {
            if (!_instance)
            {
                _instance = new GuildNewCopyPanel;
            }
            return _instance;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
